package com.mall.market.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.market.mapper.SmsFlashPromotionMapper;
import com.mall.model.domain.SmsFlashPromotion;
import com.mall.model.dto.SmsFlashPromotionDto;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.PageResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SmsFlashPromotionService {


    @Autowired
    private SmsFlashPromotionMapper smsFlashPromotionMapper;


    /**
     *
     * @param dto
     * @return
     */
    public CommonPage list(SmsFlashPromotionDto dto) {
        Page<SmsFlashPromotion> pageInfo = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<SmsFlashPromotion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(SmsFlashPromotion::getTitle, dto.getKeyword());
        smsFlashPromotionMapper.selectPage(pageInfo, queryWrapper);
        return new CommonPage(dto.getPageNum(), dto.getPageSize(),(int) pageInfo.getTotal(),pageInfo.getRecords());
    }

    public CommonPage FlashPromotionList(String keyword, Integer pageNum, Integer pageSize) {
        Page page=new Page(pageNum, pageSize);
        LambdaQueryWrapper<SmsFlashPromotion> wrapper=new LambdaQueryWrapper();
        wrapper.like(StringUtils.isNotEmpty(keyword),SmsFlashPromotion::getTitle,keyword);
        Integer count = smsFlashPromotionMapper.selectCount(wrapper);
        Page pages = smsFlashPromotionMapper.selectPage(page, wrapper);
        return new CommonPage(pageNum,pageSize,count,pages.getRecords());
    }


    public void addFlashPromotion(SmsFlashPromotion smsFlashPromotion) {
        smsFlashPromotion.setCreateTime(new Date(System.currentTimeMillis()));
        smsFlashPromotionMapper.insert(smsFlashPromotion);
    }

    public void deleteFlashPromotion(Integer id) {
        smsFlashPromotionMapper.deleteById(id);
    }

    public void updateStatus(Integer id, Integer status) {
        LambdaQueryWrapper<SmsFlashPromotion> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(id.toString()),SmsFlashPromotion::getId,id);
        SmsFlashPromotion smsFlashPromotion = smsFlashPromotionMapper.selectOne(wrapper);
        smsFlashPromotion.setStatus(status);
        smsFlashPromotionMapper.updateById(smsFlashPromotion);
    }

    public SmsFlashPromotion selectFlashPromotion(Integer id) {
        return smsFlashPromotionMapper.selectById(id);
    }

    public void updateFlashPromotion(Integer id, SmsFlashPromotion smsFlashPromotion) {
        smsFlashPromotionMapper.updateById(smsFlashPromotion);
    }
}
